﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Runtime.Serialization;

namespace Fixx.DTO
{
	[Serializable, DataContract(Name = "issue", Namespace = "")]
	public class FixxIssue : IFixxDalObject<FixxComment>
	{
		[DataMember(Name = "id", IsRequired = true, Order = 1)]
		public int Id { get; set; }

		[DataMember(Name = "title", IsRequired = true, Order = 2)]
		public string Title { get; set; }

		[DataMember(Name = "description", IsRequired = false, Order = 3)]
		public string Description { get; set; }

		[DataMember(Name = "assignedTo", IsRequired = false, Order = 4)]
		public int AssignedToId { get; set; }
		public FixxUser AssignedTo { get; set; }

		[DataMember(Name = "type", IsRequired = false, Order = 5)]
		public int IssueTypeId { get; set; }
		public FixxIssueType IssueType { get; set; }

		[DataMember(Name = "priority", IsRequired = false, Order = 6)]
		public int PriorityId { get; set; }
		public FixxPriority Priority { get; set; }

		[DataMember(Name = "area", IsRequired = false, Order = 7)]
		public int AreaId { get; set; }
		public FixxArea Area { get; set; }

		[DataMember(Name = "dueOn", IsRequired = false, Order = 8)]
		public DateTime? DueOn { get; set; }

		[DataMember(Name = "createdBy", IsRequired = false, Order = 9)]
		public int CreatedById { get; set; }
		public FixxUser CreatedBy { get; set; }
		
		[DataMember(Name = "estimatedTime", IsRequired = false, Order = 10)]
		public float EstimatedTime { get; set; }

		[DataMember(Name = "affected", IsRequired = false, Order = 11)]
		public int affectedVersionId { get; set; }
		public FixxVersion AffectedVersion { get; set; }

		[DataMember(Name = "fixfor", IsRequired = false, Order = 12)]
		public int FixForVersionId { get; set; }
		public FixxVersion FixForVersion { get; set; }

		[DataMember(Name = "resolution", IsRequired = false, Order = 13)]
		public int ResolutionId { get; set; }
		public FixxResolution Resolution { get; set; }

		[DataMember(Name = "resolvedBy", IsRequired = false, Order = 14)]
		public int resolvedById { get; set; }
		public FixxUser ResolvedBy { get; set; }

		[DataMember(Name = "resolvedOn", IsRequired = false, Order = 15)]
		public DateTime? ResolvedOn { get; set; }

		[DataMember(Name = "closed", IsRequired = false, Order = 16)]
		public bool IsClosed { get; set; }

		[DataMember(Name = "closedOn", IsRequired = false, Order = 17)]
		public DateTime? ClosedOn { get; set; }

		[DataMember(Name = "hideFromClient", IsRequired = false, Order = 18)]
		public bool HideFromClient { get; set; }
		
		public FixxIssue() { }

		
		public string ToXmlString()
		{
			XElement root = new XElement("issue");
			if (this.Id > 0)
				root.Add(new XElement("id", this.Id));
			if (this.EstimatedTime > 0)
				root.Add(new XElement("estimatedTime", this.EstimatedTime));
			if (!string.IsNullOrEmpty(this.Title))
				root.Add(new XElement("title", this.Title));
			//else throw new FixxInvalidDALException("Title is a required property for FixxIssue.");

			if (!string.IsNullOrEmpty(this.Description))
			{
				var s = this.Description.Replace("\r\n", "<br />");
				root.Add(new XElement("description", new XCData(s)));
			}
			//else throw new FixxInvalidDALException("Description is a required property for FixxIssue.");

			root.Add(new XElement("closed", this.IsClosed));
			
			if (this.AssignedTo != null)
				root.Add(new XElement("assignedTo", this.AssignedTo.Id));

			if (this.ResolvedBy != null)
				root.Add(new XElement("resolvedBy", this.ResolvedBy.Id));

			if (this.ResolvedOn.HasValue)
				root.Add(new XElement("resolvedOn", this.ResolvedOn.Value.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ")));


			
			if (this.IssueType != null)
				root.Add(new XElement("type", this.IssueType.Id));

			if (this.Priority != null)
				root.Add(new XElement("priority", this.Priority.Id));

			if (this.Area != null)
				root.Add(new XElement("area", this.Area.Id));

			//else throw new FixxInvalidDALException("Area is a required property for FixxIssue.");

			if (this.AffectedVersion != null)
				root.Add(new XElement("affected", this.AffectedVersion.Id));

			if (this.FixForVersion != null)
				root.Add(new XElement("fixfor", this.FixForVersion.Id));

			if (this.Resolution != null)
				root.Add(new XElement("resolution", this.Resolution.Id));



			return root.ToString();

		}


	}
}
